<template>
  <p class="g-name">{{goods.name}}</p>
  <p class="g-desc">{{goods.desc}}</p>
  <p class="g-price">
    <span>{{goods.price}}</span>
    <span>{{goods.oldPrice}}</span>
  </p>
  <div class="g-service">
    <dl>
      <dt>促销</dt>
      <dd>12月好物放送，App领券购买直降120元</dd>
    </dl>
    <dl>
      <dt>配送</dt>
      <dd>至
        <XtxCity @update-city='updateCity' :fullLocation='fullLocation' />
      </dd>
    </dl>
    <dl>
      <dt>服务</dt>
      <dd>
        <span>无忧退货</span>
        <span>快速退款</span>
        <span>免费包邮</span>
        <a href="javascript:;">了解详情</a>
      </dd>
    </dl>
  </div>
</template>

<script>
import { ref, toRef } from 'vue'

export default {
  name: 'GoodName',
  props: {
    goods: {
      type: Object,
      default: () => { }
    }
  },
  setup (props) {
    // 如果用户已经登录，那么通过detail.userAddresses可以获取配置地址
    // 这块主要设置默认地址
    const provinceCode = ref('110000')
    const cityCode = ref('119900')
    const countyCode = ref('110101')
    const fullLocation = ref('北京市 市辖区 东城区')
    const { goods } = toRef(props, 'goods')
    if (goods && goods.userAddresses) {
      // 证明有地址：如果有默认地址就使用默认的，否则使用第一个
      const defaultAddress = goods.userAddresses.find(item => item.isDefault === 1)
      if (defaultAddress) {
        // 有默认地址
        provinceCode.value = defaultAddress.provinceCode
        cityCode.value = defaultAddress.cityCode
        countyCode.value = defaultAddress.countyCode
        fullLocation.value = defaultAddress.fullLoaction
      } else {
        // 没有默认的使用第一个地址
        provinceCode.value = goods.userAddresses[0].provinceCode
        cityCode.value = goods.userAddresses[0].cityCode
        countyCode.value = goods.userAddresses[0].countyCode
        fullLocation.value = goods.userAddresses[0].fullLoaction
      }
      console.log(provinceCode.value, cityCode.value, countyCode.value)
    }

    // 这里是子组件传过来的城市数据,用作默认地址
    const updateCity = (cityInfo) => {
      provinceCode.value = cityInfo.provinceCode
      cityCode.value = cityInfo.cityCode
      countyCode.value = cityInfo.countyCode
      fullLocation.value = cityInfo.fullLocation
    }

    return { fullLocation, updateCity }
  }
}
</script>

<style lang="less" scoped>
.g-name {
  font-size: 22px;
}
.g-desc {
  color: #999;
  margin-top: 10px;
}
.g-price {
  margin-top: 10px;
  span {
    &::before {
      content: '¥';
      font-size: 14px;
    }
    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }
    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}
.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;
  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      color: #666;
      &:last-child {
        span {
          margin-right: 10px;
          &::before {
            content: '•';
            color: @xtxColor;
            margin-right: 2px;
          }
        }
        a {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
